﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Comod_DevolveAdd : System.Web.UI.Page
{
    ZSql rs = new ZSql();
    Function f = new Function();
    ZSql info = new ZSql();//添加修改
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           // lblName.Text = Server.UrlDecode(Request.Cookies["user"].Values["name"]);//操作人
            lblPatd.Text = f.getModuleidAdd(Request.QueryString["moduid"]);//当前位置
            hidModuid.Value = Request.QueryString["moduid"];
            HidGoUrl.Value = Server.UrlDecode(Request.UrlReferrer.ToString());
           
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))//初使化信息
            {
                hidId.Value = Request.QueryString["id"];
                initData(); 
            }
        }
    }
    private void initData()//当修改时,初使化数据
    {
        ZSql zs = new ZSql();
        string sql = "SELECT * FROM (SELECT stockId, ComodBarcode,Content,typeStock, ProcNum,WarehouseId,EditUserName,EditDate, CONVERT(char(10), storageTime, 120) AS storageTime, AuditState,(select isnull(sum(ProcNum),0) from CS_Stock where WarehouseId=a.WarehouseId and ComodBarcode=a.comodBarcode) as countNum,(select isnull(sum(number),0) from CS_Sales where ComodBarcode=a.comodBarcode and WarehouseId=a.WarehouseId and isDel=0) as saleNum,((select isnull(sum(ProcNum),0) from CS_Stock where WarehouseId=a.WarehouseId and ComodBarcode=a.comodBarcode) -(select isnull(sum(number),0) from CS_Sales where ComodBarcode=a.comodBarcode and WarehouseId=a.WarehouseId and isDel=0)) as StockNum, (SELECT WarehouseName FROM CS_Warehouse   WHERE WarehouseId = a.WarehouseId) AS WarehouseName, (SELECT Supplier FROM CS_Supplier WHERE SupplierId = a.SupplierId) AS SupplierName, (SELECT Brand  FROM Brand WHERE BrandId = a.BrandId) AS BrandName, (SELECT ComoditiesName FROM ComoditiesName  WHERE ComoditiesNameId = a.ComoditiesName) AS ComoditiesName, (SELECT Specifications FROM Specifications    WHERE SpecificationsId = a.ModelId) AS SpecificationsName, (SELECT Units FROM Units  WHERE UnitsId = a.UnitId) AS UnitsName, (SELECT ComoditiesType   FROM ComoditiesType    WHERE TypeId = a.ComoditiesType) AS ComoditiesTypeName FROM CS_Stock a ) Stock where stockId=" + Request.QueryString["id"] + " ORDER BY stockId DESC";
        zs.Open(sql);
            lblComodBarcode.Text = zs.m_table.Rows[0]["ComodBarcode"].ToString();
            lblProcNum.Text = zs.m_table.Rows[0]["ProcNum"].ToString();
            lblComoditiesTypeName.Text = zs.m_table.Rows[0]["ComoditiesTypeName"].ToString();
            LblSupplierName.Text = zs.m_table.Rows[0]["SupplierName"].ToString();
            LblBrandName.Text = zs.m_table.Rows[0]["BrandName"].ToString();
            lblComoditiesName.Text = zs.m_table.Rows[0]["ComoditiesName"].ToString();
            lblWarehouseName.Text = zs.m_table.Rows[0]["WarehouseName"].ToString();
            lblUnitsName.Text = zs.m_table.Rows[0]["UnitsName"].ToString();
            lblComoditiesName.Text = zs.m_table.Rows[0]["ComoditiesName"].ToString();
            LblSpecificationsName.Text = zs.m_table.Rows[0]["SpecificationsName"].ToString();
            lbltypeStock.Text = zs.m_table.Rows[0]["typeStock"].ToString();
            lblcountNum.Text = zs.m_table.Rows[0]["countNum"].ToString();
            lblsaleNum.Text = zs.m_table.Rows[0]["saleNum"].ToString();
            lblStockNum.Text = zs.m_table.Rows[0]["StockNum"].ToString();
            lblstorageTime.Text = zs.m_table.Rows[0]["storageTime"].ToString();
            lblContent.Text = zs.m_table.Rows[0]["Content"].ToString();
            lblName.Text = zs.m_table.Rows[0]["EditUserName"].ToString();
            lblDate.Text = zs.m_table.Rows[0]["EditDate"].ToString();
            int ProcNum = Convert.ToInt16(zs.m_table.Rows[0]["ProcNum"]);
            int StockNum=Convert.ToInt16(zs.m_table.Rows[0]["StockNum"]);
            int WarehouseId = Convert.ToInt16(zs.m_table.Rows[0]["WarehouseId"]);
            HidWarehouseid.Value = zs.m_table.Rows[0]["WarehouseId"].ToString();
        zs.Close();


        //设置转库数量
        int Num = 0;
        if (StockNum < ProcNum)//如果库存小于单次入库数;
            Num = StockNum;//则转库只能少于库存数量
        else
            Num = ProcNum;//则转库按单次入库数量
        for (int i = 1; i <= Num; i++)
        {
            ddlDevolveNum.Items.Insert(i-1, new ListItem(i.ToString(), i.ToString()));
        }
        //设置库房
        ddlComodName(WarehouseId);

    }

    protected void btnAdd_Click(object sender, EventArgs e)//系统提交保存转库内容
    {
        ZSql seleccomi = new ZSql();
        ZSql addcomi = new ZSql();
        ZSql houseupdate = new ZSql();
        seleccomi.Open("select * from CS_Stock where stockId=" + hidId.Value + " and warehouseid=" + HidWarehouseid.Value + " ");

        if (seleccomi.m_table.Rows.Count > 0)
        {
            addcomi.Open("insert into CS_Stock(WarehouseId,OldWarehouseId,ComoditiesType,ComodBarcode,SuppliId,SupplierId,BrandId,ComoditiesName,ModelId,UnitId,Prices,ProcNum,storageTime,AuditState,AuditUserName,AuditDate,EditUserName,EditDate,typeStock,typeStockId,ProcMoney,SuppliMoney,ChainMoney,WholeMoney1,WholeMoney2,RetailMoney,RestMoney,ActiveMoney,newSuppliMoney) values('" + ddlComodiName.SelectedValue + "','" + seleccomi.m_table.Rows[0]["WarehouseId"].ToString() + "','" + seleccomi.m_table.Rows[0]["ComoditiesType"].ToString() + "','" + seleccomi.m_table.Rows[0]["ComodBarcode"].ToString() + "','" + seleccomi.m_table.Rows[0]["SuppliId"].ToString() + "','" + seleccomi.m_table.Rows[0]["SupplierId"].ToString() + "','" + seleccomi.m_table.Rows[0]["BrandId"].ToString() + "','" + seleccomi.m_table.Rows[0]["ComoditiesName"].ToString() + "','" + seleccomi.m_table.Rows[0]["ModelId"].ToString() + "','" + seleccomi.m_table.Rows[0]["UnitId"].ToString() + "'," + seleccomi.m_table.Rows[0]["Prices"] + "," + ddlDevolveNum.SelectedValue + ",'" + seleccomi.m_table.Rows[0]["storageTime"] + "','" + seleccomi.m_table.Rows[0]["AuditState"].ToString() + "','" + seleccomi.m_table.Rows[0]["AuditUserName"].ToString() + "','" + seleccomi.m_table.Rows[0]["AuditDate"] + "','" + Server.UrlDecode(Request.Cookies["user"].Values["realname"]) + "','" + DateTime.Now.ToString() + "','" + seleccomi.m_table.Rows[0]["typeStock"].ToString() + "'," + seleccomi.m_table.Rows[0]["typeStockId"].ToString() + "," + seleccomi.m_table.Rows[0]["ProcMoney"] + "," + seleccomi.m_table.Rows[0]["SuppliMoney"] + "," + seleccomi.m_table.Rows[0]["ChainMoney"] + "," + seleccomi.m_table.Rows[0]["WholeMoney1"] + "," + Convert.ToDouble(seleccomi.m_table.Rows[0]["WholeMoney2"]) + "," + Convert.ToDouble(seleccomi.m_table.Rows[0]["RetailMoney"]) + "," + Convert.ToDouble(seleccomi.m_table.Rows[0]["RestMoney"]) + "," + seleccomi.m_table.Rows[0]["ActiveMoney"] + "," + seleccomi.m_table.Rows[0]["newSuppliMoney"] + ")");
           houseupdate.Open("update CS_Stock set ProcNum=ProcNum-" + ddlDevolveNum.SelectedValue.ToString() + " where stockId=" + hidId.Value + " and warehouseid=" + HidWarehouseid.Value + "");
        }
        seleccomi.Close();
        addcomi.Close();
        houseupdate.Close();
        Response.Write("<script language='javascript'>alert('转库操作成功!');location.href='" + HidGoUrl.Value + "';</script>");
    }

    protected void ddlComodName(int WarehouseId)//库房
    {
        ZSql ware = new ZSql();
        ware.Open("select warehouseid,warehousename from CS_Warehouse where AuditState='已通过' and WarehouseId<>" + WarehouseId);
        if (ware.m_table.Rows.Count > 0)
        {
            ddlComodiName.DataSource = ware.m_table;
            ddlComodiName.DataTextField = "warehousename";
            ddlComodiName.DataValueField = "warehouseid";
            ddlComodiName.DataBind();
            ware.Close();
        }
        else
        {
            Response.Write("<script>alert('仓库未审核！');</script>");
            ddlComodiName.Items.Insert(0, new ListItem("选择仓库", ""));
        }

    }

}
